"use strict";

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/* global globalRootUrl, SemanticLocalization, UserMessage,  */

/**
 * Define object which manage IVR (Interactive Voice Menu) list
 *
 * @module ivrMenuIndex
 */
var ivrMenuIndex = {
  $ivrTable: $('#ivr-menu-table'),
  initialize: function initialize() {
    // Add double click listener to table cells
    $('.menu-row td').on('dblclick', function (e) {
      // When cell is double clicked, navigate to corresponding modify page
      var id = $(e.target).closest('tr').attr('id');
      window.location = "".concat(globalRootUrl, "ivr-menu/modify/").concat(id);
    }); // Initialize the data table

    ivrMenuIndex.initializeDataTable(); // Set up delete functionality on delete button click.

    $('body').on('click', 'a.delete', function (e) {
      e.preventDefault();
      $(e.target).addClass('disabled'); // Get the ivr menu  ID from the closest table row.

      var rowId = $(e.target).closest('tr').attr('id'); // Remove any previous AJAX messages.

      $('.message.ajax').remove(); // Call the PbxApi method to delete the IVR menu record.

      IVRMenuAPI.deleteRecord(rowId, ivrMenuIndex.cbAfterDeleteRecord);
    });
  },

  /**
   * Initialize data tables on table
   */
  initializeDataTable: function initializeDataTable() {
    ivrMenuIndex.$ivrTable.DataTable({
      lengthChange: false,
      // Disable ability to change number of entries shown
      paging: false,
      // Disable pagination
      columns: [null, null, null, null, null, {
        orderable: false,
        searchable: false
      }],
      order: [1, 'asc'],
      language: SemanticLocalization.dataTableLocalisation
    }); // Move the "Add New" button to the first eight-column div

    $('#add-new-button').appendTo($('div.eight.column:eq(0)'));
  },

  /**
   * Callback function executed after deleting a record.
   * @param {Object} response - The response object from the API.
   */
  cbAfterDeleteRecord: function cbAfterDeleteRecord(response) {
    if (response.result === true) {
      // Remove the deleted record's table row.
      ivrMenuIndex.$ivrTable.find("tr[id=".concat(response.data.id, "]")).remove(); // Call the callback function for data change.

      Extensions.cbOnDataChanged();
    } else {
      // Show an error message if deletion was not successful.
      UserMessage.showError(response.messages.error, globalTranslate.iv_ImpossibleToDeleteIVRMenu);
    }

    $('a.delete').removeClass('disabled');
  }
};
/**
 *  Initialize IVR menu table on document ready
 */

$(document).ready(function () {
  ivrMenuIndex.initialize();
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9JdnJNZW51L2l2cm1lbnUtaW5kZXguanMiXSwibmFtZXMiOlsiaXZyTWVudUluZGV4IiwiJGl2clRhYmxlIiwiJCIsImluaXRpYWxpemUiLCJvbiIsImUiLCJpZCIsInRhcmdldCIsImNsb3Nlc3QiLCJhdHRyIiwid2luZG93IiwibG9jYXRpb24iLCJnbG9iYWxSb290VXJsIiwiaW5pdGlhbGl6ZURhdGFUYWJsZSIsInByZXZlbnREZWZhdWx0IiwiYWRkQ2xhc3MiLCJyb3dJZCIsInJlbW92ZSIsIklWUk1lbnVBUEkiLCJkZWxldGVSZWNvcmQiLCJjYkFmdGVyRGVsZXRlUmVjb3JkIiwiRGF0YVRhYmxlIiwibGVuZ3RoQ2hhbmdlIiwicGFnaW5nIiwiY29sdW1ucyIsIm9yZGVyYWJsZSIsInNlYXJjaGFibGUiLCJvcmRlciIsImxhbmd1YWdlIiwiU2VtYW50aWNMb2NhbGl6YXRpb24iLCJkYXRhVGFibGVMb2NhbGlzYXRpb24iLCJhcHBlbmRUbyIsInJlc3BvbnNlIiwicmVzdWx0IiwiZmluZCIsImRhdGEiLCJFeHRlbnNpb25zIiwiY2JPbkRhdGFDaGFuZ2VkIiwiVXNlck1lc3NhZ2UiLCJzaG93RXJyb3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yk7XG5cbiAgICAgICAgICAgIC8vIFJlbW92ZSBhbnkgcHJldmlvdXMgQUpBWCBtZXNzYWdlcy5cbiAgICAgICAgICAgICQoJy5tZXNzYWdlLmFqYXgnKS5yZW1vdmUoKTtcblxuICAgICAgICAgICAgLy8gQ2FsbCB0aGUgUGJ4QXBpIG1ldGhvZCB0byBkZWxldGUgdGhlIElWUiBtZW51IHJlY29yZC5cbiAgICAgICAgICAgIElWUk1lbnVBUEkuZGVsZXRlUmVjb3JkKHJvd0lkLCBpdnJNZW51SW5kZXguY2JBZnRlckRlbGV0ZVJlY29yZCk7XG4gICAgICAgIH0pO1xuICAgIH0sXG5cbiAgICAvKipcbiAgICAgKiBJbml0aWFsaXplIGRhdGEgdGFibGVzIG9uIHRhYmxlXG4gICAgICovXG4gICAgaW5pdGlhbGl6ZURhdGFUYWJsZSgpIHtcbiAgICAgICAgaXZyTWVudUluZGV4LiRpdnJUYWJsZS5EYXRhVGFibGUoe1xuICAgICAgICAgICAgbGVuZ3RoQ2hhbmdlOiBmYWxzZSwgLy8gRGlzYWJsZSBhYmlsaXR5IHRvIGNoYW5nZSBudW1iZXIgb2YgZW50cmllcyBzaG93blxuICAgICAgICAgICAgcGFnaW5nOiBmYWxzZSwgLy8gRGlzYWJsZSBwYWdpbmF0aW9uXG4gICAgICAgICAgICBjb2x1bW5zOiBbXG4gICAgICAgICAgICAgICAgbnVsbCxcbiAgICAgICAgICAgICAgICBudWxsLFxuICAgICAgICAgICAgICAgIG51bGwsXG4gICAgICAgICAgICAgICAgbnVsbCxcbiAgICAgICAgICAgICAgICBudWxsLFxuICAgICAgICAgICAgICAgIHtvcmRlcmFibGU6IGZhbHNlLCBzZWFyY2hhYmxlOiBmYWxzZX0sXG4gICAgICAgICAgICBdLFxuICAgICAgICAgICAgb3JkZXI6IFsxLCAnYXNjJ10sXG4gICAgICAgICAgICBsYW5ndWFnZTogU2VtYW50aWNMb2NhbGl6YXRpb24uZGF0YVRhYmxlTG9jYWxpc2F0aW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